/***********************************************************
* 
*     File Name:  taskEvent.cpp
* 
*        Author:  Algolc
* 
*          mail:  17360224@qq.com
* 
*  Created Time:  Tue 10 Jul 2018 05:43:06 PM CST
* 
***********************************************************/
#include <stdlib.h>
#include <unistd.h>
#include "zhelpers.h"

int main() {
    void * context = zmq_ctx_new();

    //用于发送消息的套接字
    void * sender = zmq_socket(context, ZMQ_PUSH);
    zmq_bind(sender, "tcp://*:5557");

    //用于发送批次开始消息的套接字
    void * sink = zmq_socket(context, ZMQ_PUSH);
    zmq_connect(sink, "tcp://localhost:5558");

    printf("Press Enter when the worker are ready:");
    getchar();

    printf("Sending tasks to workers... \n");

    //第一个消息时"0", 表示批次的开始
    char buf[10] = "0";
    s_send(sink, buf);
    
    //初始化随机数发生器
    srandom((unsigned)time(NULL));

    //发送100个任务
    int task_nbr;
    int total_msec = 0;
    
    for (task_nbr = 0; task_nbr < 100; ++task_nbr) {
        int workload;
        //从1到100毫秒的随机工作负载
        workload = randof(100) + 1;
        total_msec += workload;
        char string[10];
        sprintf(string, "%d", workload);
        s_send(sender, string);
    }
    printf("Total expected cost: %d msec\n", total_msec);
    sleep(1);

    zmq_close(sink);
    zmq_close(sender);

    zmq_ctx_destroy(context);
    return 0;
}
